﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace app2
{
    public partial class Form3 : Form
    {
        MyBoll2 myboll;
        List<Boll2> bolls = new List<Boll2>();
        SolidBrush sb1 = new SolidBrush(Color.Red);
        SolidBrush sb2 = new SolidBrush(Color.Blue);
        SolidBrush sb3 = new SolidBrush(Color.Black);
        Graphics g = null;
        int bollnum = 25;
        public Random rand;
        public int timer = 0;
        public Form3()
        {
            InitializeComponent();
            this.DoubleBuffered = true;  //避免闪烁
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            rand = new Random();
            myboll = new MyBoll2(this.ClientSize.Width, this.ClientSize.Height);
            for (int i = 0; i < bollnum; i++)
            {
                Boll2 boll = new Boll2(this.ClientSize.Width, this.ClientSize.Height, rand);
                bolls.Add(boll);
            }
            timerball.Start();
            timertotal.Start();
        }

        private void Form3_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            foreach (Boll2 boll in bolls)
            {
                g.FillEllipse(sb1, boll.x, boll.y, boll.d, boll.d);
            }
            g.FillEllipse(sb2, myboll.x, myboll.y, myboll.d, myboll.d);
        }

        private void timerball_Tick(object sender, EventArgs e)
        {
            foreach (Boll2 boll in bolls)
            {
                boll.Move();        
                double Y = myboll.y - boll.y;
                double X = myboll.x - boll.x;
                double R = X * X + Y * Y;
                if (R <Math.Pow(boll.d / 2 + myboll.d / 2,2))
                {
                    this.timerball.Stop();
                    this.timertotal.Stop();
                    MessageBox.Show("坚持了" + timer + "秒");
                    DialogResult a = MessageBox.Show("是否再次挑战？", "提示", MessageBoxButtons.OKCancel);
                    if (a == DialogResult.OK)
                    {
                        Form3 form = new Form3();
                        form.Show();
                        this.Hide();
                    }
                    else
                    {
                        Login login = new Login();
                        login.Show();
                        this.Hide();
                    }
                }
            }
            this.Refresh();
        }
        #region 控制球的方向，上下左右的操作
        private void Form3_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    timerup.Start();
                    break;
                case Keys.Down:
                    timerdown.Start();
                    break;
                case Keys.Left:
                    timerleft.Start();
                    break;
                case Keys.Right:
                    timerright.Start();
                    break;
                default: break;
            }
        }

        private void Form3_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    timerup.Stop();
                    break;
                case Keys.Down:
                    timerdown.Stop();
                    break;
                case Keys.Left:
                    timerleft.Stop();
                    break;
                case Keys.Right:
                    timerright.Stop();
                    break;
                default: break;
            }
        }

        private void timerup_Tick(object sender, EventArgs e)
        {
            myboll.Up();
        }

        private void timerleft_Tick(object sender, EventArgs e)
        {
            myboll.Left();
        }

        private void timerright_Tick(object sender, EventArgs e)
        {
            myboll.Right();
        }

        private void timerdown_Tick(object sender, EventArgs e)
        {
            myboll.Down();
        }
        #endregion

        private void timertotal_Tick(object sender, EventArgs e)
        {
            timer++;
        }
    }
}
